package club.easin.sq.bl;

import club.easin.MYException;
import club.easin.common.DatagridRequestDto;
import club.easin.enums.Exenum;
import club.easin.pow.dto.UserSession;
import club.easin.sq.dao.SqHouseMapper;
import club.easin.sq.dto.HouseQueryDto;
import club.easin.sq.dto.SqHouse;
import club.easin.utils.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.netty.util.internal.EmptyPriorityQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class Housebl {

    @Autowired
    private SqHouseMapper sqHouseMapper;
    @Autowired
    private DBSubmit dbSubmit;

    public ReturnDto addHouse(UserSession userSession, SqHouse sqHouse){
        Tlist tlist=new Tlist();
        Date now= DateUtils.getNow();
        sqHouse.setHouseid(Commutils.buildId());
        sqHouse.setCreatetime(now);
        sqHouse.setUpdatetime(now);
        sqHouse.setCreateuser(userSession.getUser().getUserid());
        tlist.add(SqHouseMapper.class,"insert",sqHouse);
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
    }

    public ReturnDto delHouse(String houseid){
        Tlist tlist=new Tlist();
        String[] ids=houseid.split(",");
        for(String id:ids){
            if(sqHouseMapper.getInmateCount(id)>0){
                throw new MYException(Exenum.HOUSEHASINMATE);
            }
            tlist.add(SqHouseMapper.class,"deleteByPrimaryKey",id);
        }
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
    }

    public ReturnDto editHouse(SqHouse sqHouse){
        Tlist tlist=new Tlist();
        sqHouse.setUpdatetime(DateUtils.getNow());
        tlist.add(SqHouseMapper.class,"updateByPrimaryKeySelective",sqHouse);
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
    }

    public ReturnDto houseGrild(DatagridRequestDto<HouseQueryDto> dto){
        Page<SqHouse> page=null;
        PageHelper.startPage(dto.getPagenum(),dto.getPagesize());
        HouseQueryDto houseQueryDto=dto.getData();
        page=sqHouseMapper.getGrildHouse(houseQueryDto);
        return ReturnUtils.returnsuccess(DataGrildUtils.returnDataGrild(page));
    }

    public ReturnDto addInamteToHouse(String houseid,String inmateid){
        Tlist tlist=new Tlist();
        String[] inmateids=inmateid.split(",");
        for(String id:inmateids){
            tlist.add(SqHouseMapper.class,"addInmateToHouse",houseid,id);
        }
        dbSubmit.exce(tlist);
        return  ReturnUtils.returnsuccess();
    }

    public ReturnDto getInamteByHouse(String houseid){
        return ReturnUtils.returnsuccess(sqHouseMapper.getInamteByHouse(houseid));
    }

    public ReturnDto delHouseInmate(String houseid,String inmateid){
        Tlist tlist=new Tlist();
        String[] ids=inmateid.split(",");
        for (String id:ids){
            sqHouseMapper.delHouseInmate(houseid,inmateid);
            tlist.add(SqHouseMapper.class,"delHouseInmate",houseid,inmateid);
        }
        dbSubmit.exce(tlist);
        return ReturnUtils.returnsuccess();
    }
}
